import { request } from '@/service/request';

/** Doris存储元数据 */
export interface DorisStorage {
  id?: number;
  tenantId?: number;
  databaseName?: string;
  tableName?: string;
  tableType?: string;
  tableComment?: string;
  tableSchema?: string;
  rowCount?: number;
  dataSize?: number;
  partitionCount?: number;
  createTime?: string;
  updateTime?: string;
  lastSyncTime?: string;
}

/** Doris分区信息 */
export interface DorisPartition {
  id?: number;
  storageId?: number;
  tenantId?: number;
  partitionName?: string;
  partitionType?: string;
  partitionValue?: string;
  rowCount?: number;
  dataSize?: number;
  createTime?: string;
  updateTime?: string;
}

/** 数据库树节点 */
export interface DorisDatabaseTree {
  id: string;
  label: string;
  type: 'database' | 'table' | 'partition';
  databaseName?: string;
  tableName?: string;
  partitionName?: string;
  storageId?: number;
  partitionId?: number;
  partitionCount?: number;
  children?: DorisDatabaseTree[];
}

/** 表结构 */
export interface DorisTableStructure {
  fieldName: string;
  fieldType: string;
  nullable: boolean;
  defaultValue?: string;
  comment?: string;
  isPrimaryKey: boolean;
}

/** 表统计信息 */
export interface DorisTableStatistics {
  databaseName: string;
  tableName: string;
  rowCount: number;
  dataSize: number;
  dataSizeFormatted: string;
  partitionCount: number;
  replicaCount?: number;
  tableType: string;
}

/** 数据预览 */
export interface DorisDataPreview {
  columns: string[];
  rows: Record<string, any>[];
  total: number;
  pageNum: number;
  pageSize: number;
}

/**
 * 查询Doris存储元数据列表
 */
export function getDorisStorageList(params?: any) {
  return request<Api.Common.PaginatingQueryRecord<DorisStorage>>({
    url: '/dataplatform/doris-storage/list',
    method: 'get',
    params
  });
}

/**
 * 获取Doris存储元数据详情
 */
export function getDorisStorageDetail(id: number) {
  return request<DorisStorage>({
    url: `/dataplatform/doris-storage/${id}`,
    method: 'get'
  });
}

/**
 * 同步Doris元数据
 */
export function syncDorisMetadata() {
  return request({
    url: '/dataplatform/doris-storage/sync',
    method: 'post'
  });
}

/**
 * 获取数据库树形结构
 */
export function getDatabaseTree() {
  return request<DorisDatabaseTree[]>({
    url: '/dataplatform/doris-storage/tree',
    method: 'get'
  });
}

/**
 * 查询表结构
 */
export function getTableStructure(databaseName: string, tableName: string) {
  return request<DorisTableStructure[]>({
    url: '/dataplatform/doris-storage/structure',
    method: 'get',
    params: { databaseName, tableName }
  });
}

/**
 * 查询表统计信息
 */
export function getTableStatistics(databaseName: string, tableName: string) {
  return request<DorisTableStatistics>({
    url: '/dataplatform/doris-storage/statistics',
    method: 'get',
    params: { databaseName, tableName }
  });
}

/**
 * 预览表数据
 */
export function previewTableData(databaseName: string, tableName: string, pageNum?: number, pageSize?: number) {
  return request<DorisDataPreview>({
    url: '/dataplatform/doris-storage/preview',
    method: 'get',
    params: { databaseName, tableName, pageNum, pageSize }
  });
}

/**
 * 查询分区列表
 */
export function getPartitionList(storageId: number) {
  return request<DorisPartition[]>({
    url: `/dataplatform/doris-storage/partition/list/${storageId}`,
    method: 'get'
  });
}

/**
 * 删除分区
 */
export function deletePartition(databaseName: string, tableName: string, partitionName: string) {
  return request({
    url: '/dataplatform/doris-storage/partition',
    method: 'delete',
    params: { databaseName, tableName, partitionName }
  });
}
